import sys
sys.path.append("/home/sshadmin/workspace/trilolo")
# sys.path.append("/home/sshadmin/music/Logging/")
from flask import Flask
from flask import request
import ast
import json
from controller import Controller
import traceback
import urllib
from logger import log
from flask import render_template
app = Flask(__name__,template_folder='/home/sshadmin/workspace/trilolo/templates',static_url_path = "/static", static_folder = "/home/sshadmin/workspace/trilolo/static_folder")
from flask import make_response,redirect
from globals.ErrorClasses import AuthenticationError
import cgi

@app.route('/' , methods=['GET'])     
def main():
    log.info("NEW REQUEST FOR main , DATA RECIEVED :" + request.data )
    try:
        user_id = request.cookies.get('user_id')
        facebook_id = request.cookies.get('facebook_id')
        facebook_code=request.args.get("code")
        track=request.args.get("track")
        show=request.args.get("s")
        controller=Controller(user_id=user_id,facebook_id=facebook_id,facebook_code=facebook_code)
        log.debug("user "+str(user_id))
        track=controller.getTrack(track,show)
        log.debug("get track response "+str(track))
        return track
    except AuthenticationError:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    except:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
@app.route('/get_track' , methods=['GET'])     
def getTrack():
    log.info("NEW REQUEST FOR get track , DATA RECIEVED :" + request.data )
    try:
        user_id = request.cookies.get('user_id')
        facebook_id = request.cookies.get('facebook_id')
        facebook_code=request.args.get("code")
        controller=Controller(user_id=user_id,facebook_id=facebook_id,facebook_code=facebook_code)
        log.debug("user "+str(user_id))
        track=controller.getOnetrack()
        log.debug("get track response "+str(track))
        return json.dumps(track)
    except AuthenticationError:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    except:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    
@app.route('/logout' , methods=['GET'])     
def logout():
    log.info("NEW REQUEST FOR logout , DATA RECIEVED :" + request.data )
    try:
        user_id = request.cookies.get('user_id')
        facebook_id = request.cookies.get('facebook_id')
        facebook_code=request.args.get("code")
        controller=Controller(user_id=user_id,facebook_id=facebook_id,facebook_code=facebook_code)
        return controller.logout()
    except AuthenticationError:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    except:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    
@app.route('/login' , methods=['GET'])     
def login():
    log.info("NEW REQUEST FOR login , DATA RECIEVED :" + request.data )
    try:
        user_id = request.cookies.get('user_id')
        facebook_id = request.cookies.get('facebook_id')
        args = dict(client_id='158218584351709', redirect_uri="http://www.trilolo.com/")
        log.debug("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
        return redirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
    except AuthenticationError:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    except:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
@app.route('/log_track' , methods=['GET'])     
def logTrack():
    log.info("NEW REQUEST FOR log track , DATA RECIEVED :" + request.data )
    try:
        user_id = request.cookies.get('user_id')
        facebook_id = request.cookies.get('facebook_id')
        facebook_code=request.args.get("code")
        track = request.args.get('track')
        action = request.args.get('action')
        position = request.args.get('pos')
        log.debug("position "+str(position))
        controller=Controller(user_id=user_id,facebook_id=facebook_id,facebook_code=facebook_code)
        log.debug("user "+str(user_id))
        track=controller.logTrack(track=track,action=action,position=position)
        return json.dumps([])
    except AuthenticationError:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400
    except:
        log.error(traceback.format_exc())
        return json.dumps('[]'), 400    
    
if __name__ == "__main__":
    app.debug = True
    app.run(use_reloader=True, host='0.0.0.0',port=5001)
    
